/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.Institucion;

import BD.BaseDatos;
import com.myapp.struts.Carrera.ConsultarCarreraForm;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Andreina Wilhelm
 */
public class ConsultarInstitucionAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        // crea instancia de BD
        BaseDatos bd = new BaseDatos();

        ConsultarInstitucionForm cif = (ConsultarInstitucionForm) form;
        String id = cif.getInstitucionID();

        try {
            // establece conexion con BD
            bd.establecerConexion();

            // se crea el query
            String query = "SELECT * FROM institucion WHERE id = '" + id + "'";

            // se ejecuta query
            ResultSet rs = bd.ejecutarQuery(query);

            if (rs.next()) { 
                // Se agrega la institucion seleccionada al form de consultar institucion
                cif.setInstitucionSelected(new Institucion(Integer.parseInt(rs.getString("id")), rs.getString("acronimo"), rs.getString("nombre"), rs.getString("sede"), rs.getString("pais")));
            }

            // Se termina conexion con BD
            bd.terminarConexion();

            
            // obtengo de sesion la CARRERA seleccionada y le asigno null xq estar aqui quiere decir q cambie la seleccion de institucion
            HttpSession sesion = request.getSession();
            ConsultarCarreraForm ccf = (ConsultarCarreraForm) sesion.getAttribute("ConsultarCarreraForm");

            if (ccf != null) {
                // Se coloca la carrera seleccionada igual a null xq ahora seleccionare una nueva (cambie de institucion)
                ccf.setCarreraSelected(null);
                sesion.setAttribute("ConsultarCarreraForm", ccf);
            }

            return mapping.findForward(SUCCESS);

        } catch (Exception e) {
            System.out.println("ERROR: Problema al consultar la BD");
        }

        // Se termina conexion con BD
        bd.terminarConexion();
            
        return mapping.findForward(FAILURE);
    }
}
